<?php

class WP_Widget_Random extends WP_Widget{	
	function __construct(){
		$widget_ops=array('classname'=>'sbkk layout5','description'=>"随便看看");
		parent::__construct(false, "随便看看", $widget_ops);
	}
	
	function widget($args,$instance){
		extract($args);
		$title=apply_filters('widget_title', '随便看看', $instance, $this->id_base);
		$cat=!empty($instance['cat'])?$instance['cat']:'1';
		echo $before_widget;
		if($title){
			echo $before_title.$title.$after_title;
		}
		//内容
		self::getHtml("showPosts=$cat");
		echo $after_widget;
	}
	
	function update($new_instance,$old_instance){
		$instance = $old_instance;
		$instance['cat'] = strip_tags(stripslashes($new_instance['cat']));;
		return $instance;
	}
	
	function form($instance){
		$instance = wp_parse_args((array)$instance,array('cat'=>'1'));
		$cat= strip_tags($instance['cat']);
		?>
		<p><label for="<?php echo $this->get_field_id('cat'); ?>">分类编号</label> <input class="widefat" id="<?php echo $this->get_field_id('cat'); ?>" name="<?php echo $this->get_field_name('cat'); ?>" type="text" value="<?php echo $cat; ?>" /></p>
	<?php 
	}
	
	function getHtml($args=''){		
		$default=array('cat'=>"1");
		$parse_args=wp_parse_args($args,$default);
		extract($parse_args);
		$args_post = array(
			'orderby' => post_date,
		    'posts_per_page' =>16,
		    'ignore_sticky_posts'=>1,
			'cat'=>$parse_args['cat']
		);
		$post_query = new WP_Query($args_post);
		if($post_query->have_posts()):
			echo '<div class="bd"><ul class="newslist">';
			while($post_query->have_posts()):
				$post_query->the_post();
				echo '<li>';
                echo '<a class="info" href="'.the_permalink().'">'.get_the_title().'</a>';
                echo '</li>';
			endwhile;
			echo '</ul></div>';
			
		 endif;		
		wp_reset_query();
	}
}
?>